﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;

namespace codeQR_generator
{
    class Program
    {
        
        private static Bitmap objBitmap;
        private static Graphics objGraphics;

        static void drawAlignmentPattern(int x, int y) {
            if (!(x == 3 && y == 3))
            {
                Rectangle r1 = new Rectangle(x+1, y+1, 4, 4);
                Rectangle r1_cent = new Rectangle(x + 2, y + 2, 3, 3);
                Rectangle r1_cent_ = new Rectangle(x + 3, y + 3, 1, 1);

                SolidBrush b1 = new SolidBrush(Color.White);
                SolidBrush b2 = new SolidBrush(Color.Black);
                Pen p1 = new Pen(Color.Black);
                objGraphics.DrawRectangle(p1, r1);
                objGraphics.FillRectangle(b1, r1_cent);
                objGraphics.FillRectangle(b2, r1_cent_);
            }
        }
        static void Main(string[] args)
        {
            

            objBitmap = new Bitmap(178,178);
            
            objGraphics = Graphics.FromImage(objBitmap);


            objGraphics.Clear(Color.White);

         
            Rectangle r1 = new Rectangle(0, 0, 6, 6);
            Rectangle r2 = new Rectangle(170, 0, 6, 6);
            Rectangle r3 = new Rectangle(0, 170, 6, 6);
            Rectangle r1_cent = new Rectangle(2, 2, 3, 3);
            Rectangle r2_cent = new Rectangle(172,2, 3, 3);
            Rectangle r3_cent = new Rectangle(2, 172, 3, 3);
           SolidBrush b1 = new SolidBrush(Color.Black);
           Pen p1 = new Pen(Color.Black);
           objGraphics.DrawRectangle(p1, r1);
           objGraphics.DrawRectangle(p1, r2);
           objGraphics.DrawRectangle(p1, r3);
           objGraphics.FillRectangle(b1, r1_cent);
           objGraphics.FillRectangle(b1, r2_cent);
           objGraphics.FillRectangle(b1, r3_cent);

           #region oldCode
           //for (int count = 7; count < 170; count++)
           //{
           //    if (count % 2 == 0)
           //    {
           //        objBitmap.SetPixel(count, 6, Color.Black);
           //        objBitmap.SetPixel( 6,count, Color.Black);
           //    }
           //    else {
           //        objBitmap.SetPixel(count, 6, Color.White);
           //        objBitmap.SetPixel(6, count, Color.White);
           //    }
           //    if ((count - 7) % 20 == 0 && (count - 7) >= 20 && (count - 7) < 160)
           //    {

           //        drawAlignmentPattern(count-1, 4);
           //        drawAlignmentPattern(4, count-1);
           //        if ((count - 7) % 20 == 0)
           //        {
           //            //&& (count - 7) >= 20 && (count - 7) < 160

           //            drawAlignmentPattern(count - 1, 4);
           //            // drawAlignmentPattern(4, count-1);

           //            for (int rowCount = 7; rowCount < 170; rowCount++)
           //            {
           //                if ((rowCount - 7) % 20 == 0)
           //                {
           //                   // drawAlignmentPattern(rowCount - 1,4 );
           //                }
           //            }
           //        }
                   
           //    }
           //}

           #endregion
           for (int i = 7; i < 169; i++) {
               if (i % 2 == 0)
               {
                   objBitmap.SetPixel(i, 6, Color.Black);
                   objBitmap.SetPixel(6, i, Color.Black);
               }
               else
               {
                   objBitmap.SetPixel(i, 6, Color.White);
                   objBitmap.SetPixel(6, i, Color.White);
               }
               for (int j = 7; j < 169; j++) {

                   if ((i - 7) % 20 == 0 && (j - 7) % 20 == 0 && (i - 7) < 160 && (j - 7) < 160)
                   { 
                  
                       drawAlignmentPattern(i-4 , j-4);
                   }
               }
           
           }
           objBitmap.Save("x.png", ImageFormat.Png);

            objBitmap.Dispose();
            objGraphics.Dispose(); 



        }
    }
}
